package saucaca.fragment;

import java.util.ArrayList;
import java.util.List;

import lemon.kenhnhacvn.BaseActivity;
import lemon.kenhnhacvn.BaseEngine;
import lemon.kenhnhacvn.R;
import lemon.kenhnhacvn.VideoPlayActivity;

import org.json.JSONException;

import saucaca.JSON.JsonReader;
import saucaca.JSON.WServiceLib;
import saucaca.Object.SongObject;
import saucaca.Util.CMD;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class VideoDetaisFragment extends Fragment {
	TextView txtTitle, txtViewed, txtLike;
	ListView listView;
	CustomAdapterVideoListview adapter;
	ArrayList<SongObject> listsongs;
	Context context;
	SongObject songObject;

	public VideoDetaisFragment(Context context,
			ArrayList<SongObject> listsongs, SongObject songObject) {
		this.context = context;
		this.songObject = songObject;
		this.listsongs = new ArrayList<SongObject>();
		for (int i = 0; i < listsongs.size(); i++) {
			if (!listsongs.get(i).getPATCH_FULL()
					.equals(songObject.getPATCH_FULL()))
				this.listsongs.add(listsongs.get(i));
		}
	}

	public VideoDetaisFragment(Context context, SongObject songObject) {
		this.context = context;
		this.songObject = songObject;
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.video_details_listsuggest, null);
		findviewById(view);
		txtTitle.setText(songObject.getTXNAME());
		
		if (songObject.getFormNet()) {
			txtViewed.setText(BaseEngine.convertStringLongtoInteger(songObject
					.getISVIEW()));
			txtLike.setText(BaseEngine.convertStringLongtoInteger(songObject
					.getISLIKE()));
			new songDetailsServices().execute("");
		} else {
			this.listsongs = new ArrayList<SongObject>();
			for (int i = 0; i < BaseActivity.allVideoMap.size(); i++) {
				if (!((SongObject)BaseActivity.allVideoMap.get(i).get(BaseActivity.Key_Music)).getPATCH_FULL().equals(songObject.getPATCH_FULL()))
					listsongs.add((SongObject)BaseActivity.allVideoMap.get(i).get(BaseActivity.Key_Music));
			}
			txtViewed.setVisibility(View.GONE);
			txtLike.setVisibility(View.GONE);
			adapter = new CustomAdapterVideoListview(context, listsongs);
			listView.setAdapter(adapter);
		}
		return view;
	}

	void findviewById(View view) {
		txtTitle = (TextView) view.findViewById(R.id.video_details_title);
		txtViewed = (TextView) view
				.findViewById(R.id.video_details_textluotnghe);
		txtLike = (TextView) view.findViewById(R.id.video_details_textYeuthich);
		listView = (ListView) view.findViewById(R.id.video_details_listview);
	}

	class songDetailsServices extends AsyncTask<String, String, String> {
		@Override
		protected String doInBackground(String... params) {
			String str = WServiceLib.idDetais(CMD.CMD_SONG_DETAIL,
					songObject.getSONG_ID());
			BaseEngine.showLogformLongString(str);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			if (result != null)
				try {
					ArrayList<SongObject> listsongs = JsonReader
							.readListsong(result);
					adapter = new CustomAdapterVideoListview(context,
							listsongs);
					listView.setAdapter(adapter);
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
	}

	public class CustomAdapterVideoListview extends ArrayAdapter<SongObject> {
		Context context;
		List<SongObject> data = new ArrayList<SongObject>();
		ImageLoader imageLoader = ImageLoader.getInstance();
		DisplayImageOptions options;
		boolean isBXH = false;
		public CustomAdapterVideoListview(Context context, List<SongObject> data) {
			super(context, R.layout.item_bxh_listview, data);
			this.context = context;
			this.data = data;
			this.isBXH = isBXH;
			options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.ic_default).showStubImage(R.drawable.ic_default).showImageForEmptyUri(R.drawable.ic_default)
					.cacheInMemory().cacheOnDisc().build();
		}
		void setListVideo(List<SongObject> data){
			this.data=data;
			notifyDataSetChanged();
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			RecordHolder holder = null;
			if (row == null) {
				LayoutInflater inflater = ((Activity) context).getLayoutInflater();
				row = inflater.inflate(R.layout.item_bxh_listview, parent, false);
				holder = new RecordHolder();
				holder.relati_Rank = (RelativeLayout) row.findViewById(R.id.item_bxh_relativeRank);
				holder.relati_Yeuthich = (RelativeLayout) row.findViewById(R.id.item_bxh_retaliYeuthich);
				holder.txtTitle = (TextView) row.findViewById(R.id.item_bxh_title);
				holder.txtArtist = (TextView) row.findViewById(R.id.item_bxh_artist);
				holder.imageItem = (ImageView) row.findViewById(R.id.item_bxh_imgAvatar);
				holder.txtLuotnghe = (TextView) row.findViewById(R.id.item_bxh_textluotnghe);
				holder.txtYeuthich = (TextView) row.findViewById(R.id.item_bxh_textYeuthich);
				holder.txtRank = (TextView) row.findViewById(R.id.item_bxh_textViewRank);
				holder.imgVideoicon = (ImageView) row.findViewById(R.id.item_bxh_Videoicon);
				holder.imgRank = (ImageView) row.findViewById(R.id.item_bxh_imgRankColor);
				row.setTag(holder);
			} else {
				holder = (RecordHolder) row.getTag();
			}
			final SongObject item = data.get(position);
			imageLoader.displayImage(item.getAVATAR(), holder.imageItem, options);
			if (isBXH == false) {
				holder.relati_Rank.setVisibility(View.INVISIBLE);
				holder.relati_Yeuthich.setVisibility(View.INVISIBLE);
			}
			holder.txtTitle.setText(item.getTXNAME());
			holder.txtArtist.setText(item.getNAME_SINGERS());
			holder.txtRank.setText(Integer.toString((position + 1)));
			holder.txtLuotnghe.setText(BaseEngine.convertStringLongtoInteger(item.getISVIEW()));
			holder.txtYeuthich.setText("0");
//			BaseEngine.setVideoclick(context, row, item);
			row.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					songObject=item;
					txtTitle.setText(item.getTXNAME());
					((VideoPlayActivity)context).songObject=item;
					((VideoPlayActivity)context).setVideo();
					if(!songObject.getFormNet()){
					listsongs.clear();
						for (int i = 0; i < BaseActivity.allVideoMap.size(); i++) {
							if (!((SongObject)BaseActivity.allVideoMap.get(i).get(BaseActivity.Key_Music)).getPATCH_FULL().equals(songObject.getPATCH_FULL()))
								listsongs.add((SongObject)BaseActivity.allVideoMap.get(i).get(BaseActivity.Key_Music));
							setListVideo(listsongs);
						}
					}
				}
			});
			return row;
		}

		class RecordHolder {
			RelativeLayout relati_Rank;
			RelativeLayout relati_Yeuthich;
			ImageView imgRank;
			ImageView imgVideoicon;
			ImageView imageItem;
			TextView txtLuotnghe;
			TextView txtYeuthich;
			TextView txtRank;
			TextView txtTitle;
			TextView txtArtist;
		}
	}

}
